package com.example.javamaday.leetcode;

public class LeetCodeTest0121 {

    public static void main(String[] args) {

    }

    public int maxProfit(int[] prices) {
        if (prices.length < 2) {
            return 0;
        }
        int[] minResult = new int[prices.length];
        minResult[0] = prices[0];
        for (int i = 1; i < prices.length; i++) {
            minResult[i] = Math.min(minResult[i - 1], prices[i]);
        }
        int max = 0;
        int[] maxProfit = new int[prices.length];
        maxProfit[0] = 0;
        for (int i = 1; i < maxProfit.length; i++) {
            maxProfit[i] = Math.max(maxProfit[i - 1], prices[i] - minResult[i - 1]);
            max = Math.max(maxProfit[i], max);
        }
        return max;
    }


}
